package com.shunhe.storage.ui.order_receive;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.arad.util.UIUtils;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.RepoRecord;

import java.util.ArrayList;
import java.util.List;

// 出库接单
public class ReceiveOrderOut1Fragment extends BaseFragment {
    private RecyclerView rcv_list;// 出库单列表

    private ReceiveOutOrderAdapter adapter;
    private List<RepoRecord> recordList = new ArrayList<>();
    private String status = "0";

    public ReceiveOrderOut1Fragment() {

    }

    public static ReceiveOrderOut1Fragment newInstance(String status) {
        ReceiveOrderOut1Fragment fragment = new ReceiveOrderOut1Fragment();
        Bundle args = new Bundle();
        args.putString("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        status = getArguments().getString("status");

        adapter = new ReceiveOutOrderAdapter(getActivity());
        for (int i = 0; i < 5; i++) {
            recordList.add(new RepoRecord());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_receive_order_in, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        LinearItemDecoration divider = new LinearItemDecoration.Builder(getActivity())
                .setSpan(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .setShowLastLine(true)
                .build();
        rcv_list.addItemDecoration(divider);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(recordList);

        setListeners();

        return v;
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                startActivity(ReceiveOrderOutDetailActivity.class);
            }
        });
    }

    private class ReceiveOutOrderAdapter extends ListBaseAdapter<RepoRecord> {

        public ReceiveOutOrderAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            if (TextUtils.equals("0", status)) {
                return R.layout.item_receive_repo_out_record_list;
            } else if (TextUtils.equals("1", status)) {
                return R.layout.item_receive_repo_out_record_list;
            }
            return R.layout.item_receive_repo_out_record_list;
        }

        @Override
        public int getItemViewType(int position) {
            if (TextUtils.equals("0", status)) {
                return 0;
            } else if (TextUtils.equals("1", status)) {
                return 1;
            } else {
                return 0;
            }
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_receive = holder.getView(R.id.tv_receive);
            TextView tv_status = holder.getView(R.id.tv_status);

            if (TextUtils.equals("0", status)) {
                tv_receive.setVisibility(View.VISIBLE);
                tv_receive.setText("接收订单");
                tv_status.setText("待接单");
            } else if (TextUtils.equals("1", status)) {
                tv_receive.setVisibility(View.INVISIBLE);
                tv_receive.setText("到货确认");
                tv_status.setText("已接收");
            } else {
                tv_receive.setVisibility(View.INVISIBLE);
                tv_receive.setText("");
                tv_status.setText("已处理");
            }

            tv_receive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.equals("0", status)) {
                        UIUtils.showAlertDialog(getChildFragmentManager(), "温馨提示"
                                , "是否接收该订单?",
                                "确定", "取消",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        ToastUtils.showShort("接单成功");
                                    }
                                }, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                });
                    }
                }
            });
        }
    }
}